package com.hkts.flow.manager.api;

import com.dragon.tools.common.ReturnCode;
import com.dragon.tools.common.UUIDGenerator;
import com.dragon.tools.vo.ReturnVo;
import com.hkts.flow.api.flowable.StartProcessInstanceVo;
import com.hkts.flow.api.flowableTo.HontUserTo;
import com.hkts.flow.api.flowableTo.PerformanceTo;
import com.hkts.flow.core.service.flowable.IFlowableProcessInstanceService;
import com.hkts.flow.feign.FeignInterface;
import com.hkts.flow.feign.FeignPerformance;
import com.hkts.flow.manager.utils.Result;
import com.hkts.flow.manager.utils.ResultUtil;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.idm.api.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassNameAppealResource
 * @Description  员工申诉需要的接口
 * @Author yck
 * @Date2020-12-22 8:59
 * @Version V1.0
 **/
@RestController
@RequestMapping("/rest/appeal")
public class AppealResource  extends BaseResource{
    private static Logger logger = LoggerFactory.getLogger(AppealResource.class);
    private final String nameSpace = "appeal";
    @Autowired
    private FeignInterface feignInterface;
    @Autowired
    private FeignPerformance feignPerformance;
    @Autowired
    private IFlowableProcessInstanceService flowableProcessInstanceService;
    //启动流程
    @PostMapping("/add")
    @Transactional
    public Result add(PerformanceTo performance, String sessionId) throws Exception {
        HontUserTo hontUserTo = feignInterface.UserInfo();
        String purchaseId = UUIDGenerator.generate();
        StartProcessInstanceVo startProcessInstanceVo = new StartProcessInstanceVo();
        startProcessInstanceVo.setBusinessKey(purchaseId);
        User user = getLoginUser();
        startProcessInstanceVo.setCreator(hontUserTo.getId());
        startProcessInstanceVo.setCurrentUserCode(hontUserTo.getId());
        startProcessInstanceVo.setFormName("绩效申诉流程");
        startProcessInstanceVo.setSystemSn("flow");
        startProcessInstanceVo.setProcessDefinitionKey("appeal");
        Map<String, Object> variables = new HashMap<>();
        variables.put("userOrgCode",hontUserTo.getDepartmentId());
        variables.put("userTeamCode",hontUserTo.getDepartmentCopyId());
        startProcessInstanceVo.setVariables(variables);
        ReturnVo<ProcessInstance> returnStart = flowableProcessInstanceService.startProcessInstanceByKey(startProcessInstanceVo);
        if (returnStart.getCode().equals(ReturnCode.SUCCESS)) {
            String processInstanceId = returnStart.getData().getProcessInstanceId();
            performance.setProcessInstanceId(processInstanceId);
        }
        Result result = feignPerformance.addNewAssessmentScore(performance);
        if (result.getCode() != 200) {
            throw new Exception("远程调用任务失败！");
        }
        return ResultUtil.success("提交成功！");
    }
}
